'''
Created on 09/02/2013

@author: Ethan
'''
from MaltegoTransform import *
import os
from ToMaltego import add_category
from BibTeXParser import  BibTeXParser


'''Variables'''
transform = MaltegoTransform()
parser = BibTeXParser()


'''Environment Settings'''
#Change this to local bib file location
bibtexfile = "C:\\Users\\Ethan\\Eclipse\\bibtex-to-maltego\\test.bib"
#Grabs the Name of the author from Maltego
name = str(sys.argv[1])


'''Helper Functions'''
def find(f, seq):
    k = -1
    for item in seq:
        k = k+1
        if f == item: 
            return k
    return 0

def is_bibtexfile(filecheck):
    return filecheck.endswith(".bib")


'''Main Transformation'''
def do_transformation():
    transform.addUIMessage("Category transformation from Mendelay beginning.")
    
    #For each record in the bibTeX file
    count = 0
    while count < len(results):
        #Get the next record
        biblography = results[count]
        #Get the Title from the record
        namecheck = biblography['title']
        #If the Paper title is within the Record
        if name in namecheck:
            #Check to see if keywords exist
            if "tags" in biblography:                       
                #Get the keywords of the Record
                tags = biblography['tags']                  
                #If keywords are not empty
                if tags <> "":
                    #Split the keywords into an array
                    tagarray = tags.split(",")                  
                    #Set the counter
                    i = 0                                       
                    #Cycle through the keyword array
                    while i < len(tagarray):  
                        #Grab Category Entry                  
                        catentry = tagarray[i]
                        #Provide a notification of the Category found
                        transform.addUIMessage("Category Added: " + catentry)
                        #Add the Category to Maltego
                        add_category(transform, catentry)       
                        #Go to next Category in the keyword array
                        i = i + 1                               
        count = count + 1
        
    transform.addUIMessage("Category transformation from Mendelay completed!")


'''Main Program'''
#Ensure file specified is valid and .bib
if os.path.isfile(bibtexfile) and is_bibtexfile(bibtexfile):
    #Start main code
    try:                                                        
        #Get all the data from the .bib
        results = parser.parse(bibtexfile)                      
        #Run the transformation
        do_transformation()                                     
    #Catch any file parsing errors
    except IOError as e:                                        
        #Print error
        transform.addUIMessage("File parsing error: "+ e)       
#If file validation or .bib fails
else:                                                           
    #Print failure notification
    transform.addUIMessage("File not valid")                    

#Print Transformation Log
transform.returnOutput() 
